package com.hongdee.crm.contact.controller;

import com.github.pagehelper.PageInfo;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.StringUtil;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.contact.service.ITCrmCarContactService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/2/28.
 */
@RestController
@RequestMapping("/crm/contact")
public class TCrmCarContactController {
    @Autowired
    private ITCrmCarContactService itCrmCarContactService;

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @param start 当前页面
     * @param limit 页面大小
     * @param data  查询条件集合
     * @return Map
     * @throws Exception
     * @desc 维护记录分页查询
     * @author 徐钦勇
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "page", defaultValue = "0") int page,
                    @RequestParam(name = "limit", defaultValue = "10") int limit,
                    @RequestBody Map data) throws Exception {
        PageInfo info  = itCrmCarContactService.page(page, limit, data, uid);
        Map map = new HashMap();
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        map.put("total",info.getTotal());
        map.put("dataList",info.getList());
        return map;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc
     * @author 徐钦勇
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody Map data) throws Exception {
        EntityUtils.valid(data);
        String oid = TokenUtil.getCompanyId(uid);
        this.itCrmCarContactService.saveOrUpdate(data, oid, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * 任务维系 求助
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/seek/help/save")
    @ResponseBody
    public ResultEntity seekHelp(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestBody Map data) throws Exception {
        EntityUtils.valid(data);
        String oid = TokenUtil.getCompanyId(uid);
        this.itCrmCarContactService.seekHelpSave(data, oid, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("任务维系成功，并提交求助信息！");
        return res;
    }

    /**
     * @param
     * @return Map
     * @throws Exception
     * @desc 维系明细
     * @author 徐钦勇
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                             @RequestParam(name = "companyId", defaultValue = "") String companyId,
                             @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(companyId)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        if (!"".equals(vin)) {
            List list = this.itCrmCarContactService.info(plateNum, vin, companyId);
            res.setDataList(list);
        }
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param
     * @return Map
     * @throws Exception
     * @desc 关闭任务
     * @author 徐钦勇
     */
    @RequestMapping("/close")
    @ResponseBody
    public ResultEntity close(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestBody Map data) throws Exception {
        EntityUtils.valid(data);
        String oid = TokenUtil.getCompanyId(uid);
        this.itCrmCarContactService.close(data, oid, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * 查询出车辆客户标签
     */
    @RequestMapping("/UserTag")
    @ResponseBody
    public List UserTag(@RequestParam(name = "uid", defaultValue = "") String uid,
                        @RequestParam(name = "token", defaultValue = "") String token,
                        @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        return this.itCrmCarContactService.UserTag(vin);
    }

    /**
     * 根据VIN查询出车辆标签，同时获取标签显示名称
     *
     * @param uid
     * @param token
     * @param vin
     * @return
     * @throws Exception
     */
    @RequestMapping("/UserTag/vin")
    @ResponseBody
    public Map UserTagByVin(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        Map map = new HashMap();
        List list = itCrmCarContactService.UserTagByVin(uid, vin);
        map.put("success", true);
        map.put("dataList", list);
        map.put("msg", "查询成功!!");
        return map;
    }

    /**
     * 按查询结果导出维系信息
     */
    @RequestMapping("/exclContact")
    public ResultEntity exclContact(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody Map data
    ) throws Exception {
        ResultEntity res = new ResultEntity();
        XSSFWorkbook wb = itCrmCarContactService.exportContact(data, uid);
        String path = request.getSession().getServletContext().getRealPath(".");
        String fileId = StringUtil.UUID();
        path = path + File.separator + "upload";

        File filepack = new File(path);
        if (!filepack.exists()) {
            filepack.mkdir();
        }
        String filePath = path + File.separator + fileId + ".xlsx";
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        res.setSuccess(true);
        res.setData(fileId);
        return res;
    }


    /**
     * 车辆流失分析查询
     *
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/lossMonthStatistics")
    public ResultEntity lossMonthStatistics(@RequestParam(name = "uid", defaultValue = "") String uid,
                                            @RequestParam(name = "token", defaultValue = "") String token,
                                            @RequestBody Map data) throws Exception {
        ResultEntity res = new ResultEntity();
        List<Map<String, Object>> list = this.itCrmCarContactService.carLossMonthStatistics(uid, data);
        res.setData(list);
        res.setSuccess(true);
        res.setMsg("车辆流失分析查询成功");
        return res;
    }

    /**
     * 车辆流失分析明细查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/lossMonthStatisticsDetail")
    public Map lossMonthStatisticsDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                                         @RequestParam(name = "token", defaultValue = "") String token,
                                         @RequestParam(name = "start", defaultValue = "") String start,
                                         @RequestParam(name = "limit", defaultValue = "") String limit,
                                         @RequestBody Map data) throws Exception {
        Map<String, Object> map = this.itCrmCarContactService.carLossMonthStatisticsDetail(uid, data, start, limit);
        map.put("success", true);
        map.put("msg", "车辆流失明细查询成功!!");
//        res.setData(map);
//        res.setSuccess(true);
//        res.setMsg("车辆流失明细查询成功");
        return map;
    }


    /**
     * 车辆流失明细导出Excel
     *
     * @param uid
     * @param token
     * @param data
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/lossMonthStatisticsExcel")
    public ResultEntity lossMonthStatisticsExcel(@RequestParam(name = "uid", defaultValue = "") String uid,
                                                 @RequestParam(name = "token", defaultValue = "") String token,
                                                 @RequestBody Map data,
                                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
        ResultEntity res = new ResultEntity();
        XSSFWorkbook wb = this.itCrmCarContactService.exportExcelBycarLossMonthStatistics(uid, data);
        String path = request.getSession().getServletContext().getRealPath(".");
        String fileId = StringUtil.UUID();
        path = path + File.separator + "upload";

        File filepack = new File(path);
        if (!filepack.exists()) {
            filepack.mkdir();
        }
        String filePath = path + File.separator + fileId + ".xlsx";
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        res.setSuccess(true);
        res.setData(fileId);
        return res;
    }

    //获取录音文件路径
    @RequestMapping("/getRecordAddress")
    @ResponseBody
    public ResultEntity getRecordAddress(@RequestParam(name = "uid", defaultValue = "") String uid,
                                         @RequestParam(name = "token", defaultValue = "") String token,
                                         @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = this.itCrmCarContactService.getRecordAddress(uid, id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询成功");
        return res;
    }
}

